package logic

import (
	"context"
	"xialou_platform/services/wallet/model"
	"xialou_platform/services/wallet/rpc/internal/svc"
	"xialou_platform/services/wallet/rpc/walletClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type WalletBySourceIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewWalletBySourceIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WalletBySourceIdLogic {
	return &WalletBySourceIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 根据sourceId查询钱包
func (l *WalletBySourceIdLogic) WalletBySourceId(in *walletClient.IdBody) (*walletClient.WalletParam, error) {
	wallet, err := l.svcCtx.WalletModel.FindOneBySourceId(l.ctx, in.Id)
	if err != nil {
		if err == model.ErrNotFound {
			return &walletClient.WalletParam{}, nil
		}
		return nil, err
	}

	return &walletClient.WalletParam{
		Id:          wallet.Id,
		SourceType:  wallet.SourceType,
		SourceId:    wallet.SourceId,
		Balance:     wallet.Balance,
		Status:      wallet.Status,
		BankName:    wallet.BankName,
		BankNumber:  wallet.BankNumber,
		CreateTime:  wallet.CreateTime.Unix(),
		UpdateTime:  wallet.CreateTime.Unix(),
		AccountName: wallet.AccountName,
	}, nil
}
